package com.jingdianjichi.project.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.project.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.project.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.project.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.project.application.dto.SubjectLabelDTO;
import com.jingdianjichi.project.common.entity.Result;
import com.jingdianjichi.project.domain.bo.SubjectCategoryBo;
import com.jingdianjichi.project.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:SubjectController
 * Package:com.jingdianjichi.subject.application.controller
 * Description:
 *
 * @Author fst
 * @Create 2024/9/25 18:34
 * @Version 1.0
 */
@RestController
@RequestMapping("subject/category/")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;




    @PostMapping("add")
    public Result add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不可以为空");
            Preconditions.checkArgument(!(subjectCategoryDTO.getCategoryName().equals("")), "分类名不可以为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类的父id不能为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBOCategory(subjectCategoryDTO);

            subjectCategoryDomainService.add(subjectCategoryBo);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不可以为空");
            Preconditions.checkArgument(!(subjectCategoryDTO.getCategoryName().equals("")), "分类名不可以为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类的父id不能为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBOCategory(subjectCategoryDTO);

            subjectCategoryDomainService.update(subjectCategoryBo);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>>queryPrimaryCategoty(@RequestBody SubjectCategoryDTO subjectCategoryDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryPrimaryCategory.subjectCategoryDTO:{}", JSON.toJSONString(subjectCategoryDTO));
            }

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBOCategory(subjectCategoryDTO);

           List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryPrimaryCategory(subjectCategoryBo);

            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.converBOTODTOCategoryList(subjectCategoryBoList);
            return Result.ok(subjectCategoryDTOList);


        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>>queryPrimaryByCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryByPrimary.subjectCategoryDTO:{}", JSON.toJSONString(subjectCategoryDTO));
            }

            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类的父id不能为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBOCategory(subjectCategoryDTO);

            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBo);

            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.converBOTODTOCategoryList(subjectCategoryBoList);
            return Result.ok(subjectCategoryDTOList);


        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("delete")
    public Result delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"id不能为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBOCategory(subjectCategoryDTO);

            subjectCategoryDomainService.delete(subjectCategoryBo);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("queryCategoryAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO)
    {

        try {
            if(log.isInfoEnabled())
            {
                log.info("SubjectCategoryController.queryCategoryAndLabel.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"id不能为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBOCategory(subjectCategoryDTO);

           List<SubjectCategoryBo> subjectCategoryBoList =subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBo);

           List<SubjectCategoryDTO> subjectCategoryDTOList=new ArrayList<>();

           subjectCategoryBoList.forEach(bo->{
               SubjectCategoryDTO dto= SubjectCategoryDTOConverter.INSTANCE.convertBOToDTOCategory(bo);

               List<SubjectLabelDTO> labelDTOList = SubjectLabelDTOConverter.INSTANCE.converBOTODTOLabelList(bo.getLabelList());

               dto.setLabelDTOList(labelDTOList);

               subjectCategoryDTOList.add(dto);
           });


           return Result.ok(subjectCategoryDTOList);

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return Result.ok(new ArrayList<SubjectCategoryDTO>());
    }
}